#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project ：kuberCloudTest 
@File ：primary_storage.py
@Author ：admin
@Date ：2024/3/21 11:39 
"""
from kuberCloudTest.common.basic import basicUtils
from kuberCloudTest.common.constant import OFFSET, LIMIT, AsyncType
from kuberCloudTest.common.utils import _getDetail, _getOperationType
from kuberCloudTest.urls import Urls


class primaryStorageUtils(basicUtils):

    def __init__(self, accessToken):
        super().__init__(accessToken)


def getStorage(accessToken, storageId=None, **kwargs):
    """
    获取主存储
    :param storageId:
    :param accessToken: access token
    :param kwargs: limit:
                   offset:
                   keyword: xxx
                   searchItem: 0: name; 1: uuid;
    :return:
    """
    primary_storage_utils = primaryStorageUtils(accessToken=accessToken)
    content = primary_storage_utils.cloud_http_request.get(Urls.getStorage.value,
                                                           {
                                                               'offset': kwargs.get('offset', OFFSET),
                                                               'limit': kwargs.get('limit', LIMIT),
                                                               'searchItem': kwargs.get('searchItem', 0),
                                                               'keyword': kwargs.get('keyword', '')
                                                           })
    if storageId:
        total = content.get('total', 0)
        content = primary_storage_utils.cloud_http_request.get(
            Urls.getStorage.value, {'limit': total, 'offset': OFFSET})
        content = _getDetail(content, storageId, 'msid')
    return content


def startStorage(accessToken, storageIds):
    """
    启用主存储
    :param accessToken: access token
    :param storageIds: 主存储UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 1804
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = storageIds
    return primaryStorageUtils(accessToken=accessToken).cloud_http_request.post(Urls.startStorage.value, data=data)


def stopStorage(accessToken, storageIds):
    """
    停用主存储
    :param accessToken: access token
    :param storageIds: 主存储UUID(多个使用英文逗号分割)
    :return:
    """
    operationType = 1805
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = storageIds
    return primaryStorageUtils(accessToken=accessToken).cloud_http_request.post(Urls.stopStorage.value, data=data)
